<!DOCTYPE html>
<html>
	<head>
		<#include "/templates/commons/head.html">
		<script type="text/javascript">
			/**
			*点击添加按钮进入添加页面
			*/
			function save(){
			  	if(!$("#addForm").validateForm()){
					return;
			   	}
			  	var flag = "";
				var orgId = $("#param_orgId").val();
				Common.network("${ctx!}/OrgController/checkOrgId.do", "POST", {"param_orgId": orgId}, false, function(res) {
				    flag = res;
				});
				var method = document.addForm.method.value;
				var message = "";
			    if (method == "addSave") {
					message = "您是否确定要添加?";
					if (flag > 0) {
					    layer.msg("机构编号已存在，请重新输入！");
					    return;
					}
			    } else if (method == "editSave") {
					message = "您是否确定要修改?";
			    }
				layer.confirm(message, {
				    btn: ['确定', '取消']
					}, function() {
					    $("#cityCode").val($("#city").val() != "" ? $("#city").val() : $("#province").val()); 
					 	document.addForm.action = "${ctx!}/OrgController/"+method+".do";
					 	document.addForm.submit();
					}, function() {
				});
			}
			/**
			*点击返回按钮进入用户列表页面
			*/
			function goBack(){
				document.addForm.action = "${ctx!}/OrgController/queryList.do";
				document.addForm.submit();
			}
			function changeLevel(val) {
			    console.log(val);
			}
			//省市联动
			function initCity() {
			    var cityCodePath = "<@city cityPath="${orgBean.cityCode!''}">${cityPath}</@city>";
			    if (Common.isEmpty(cityCodePath)) {
					return;
			    }
			    var cityCodes = cityCodePath.split(",");
			    $.each(cityCodes, function(i, o) {
				    if (o == null || o == "" || o == " ") {
						cityCodes.splice(i, 1);
				    }
				});
			    var provinceCode = "";
			    var cityCode = "";
			    if (cityCodes.length >= 1) {
					provinceCode = cityCodes[0];
			    }
			    if (cityCodes.length >= 2) {
					cityCode = cityCodes[1]
			    }
			    if (!Common.isEmpty(provinceCode)) {
					Common.initSelect(document.addForm.province, provinceCode);
			    }
			    if (!Common.isEmpty(cityCode)) {
					Common.changeRegion(document.addForm.city, provinceCode)
					Common.initSelect(document.addForm.city, cityCode);
					form.render();
			    }
			}
			
			function initLevelEvent() {
			    var timeout = null;
			    $("#org_level").bind("input propertychange", function() {
					var keyWord = $(this).val().replace(/\s+/g, "");
					if ("" == keyWord || isNaN(keyWord)) {
					    layer.alert("级别只能为正整数");
					    return false;
					} else if (keyWord <= 0) {
					    layer.alert("级别只能为正整数");
					    return false;
					}
					if (timeout) {
					    clearTimeout(timeout);
					}
					timeout = setTimeout(function() {
					    initPorgSelect(keyWord);
					    timeout = null;
					}, 500);
			    });
			}
			
			function initPorgSelect(keyWord) {
			    Common.postNetwork("${ctx!}/OrgController/getPorgList.do", {"level": keyWord}, function(data) {
					if (data != null && typeof data != 'undefined' && data.code != null && typeof data.code != 'undefined' && data.code == "0000") {
					    $.each(data.result, function(i, o) {
							o.code = o.orgId;
							o.name = o.orgName;
					    });
						Common.generateSelectOptions(document.addForm.porgId, data.result, "-请选择-", "");
					} else {//所填企业高于当前企业
					    layer.msg(data.message);
					    $("#org_level").val(parseInt(data.result) + 1);
					    Common.generateSelectOptions(document.addForm.porgId, [], "-请选择-", "");
					    initPorgSelect(parseInt(data.result) + 1);
					}
			    });
			}
			
			function ready() {
			    initCity();
			    initLevelEvent();
			}
		</script>
	</head>
	<body>
		<div id="div-body" class="body">
			<div class="hid-scroller-body">
				<!-- 标题 Start -->
				<#include "/templates/commons/menuPath.html">
				<!-- 标题 End -->
				<form id="addForm" name="addForm" class="layui-form" method="post" modelAttribute="orgBean" action="/OrgController">
					<!-- list页面搜索条件 Start -->
					<input type="hidden" id="param_orgName" name="param_orgName"  value=""/>
					<input type="hidden" id="param_manager" name="param_manager"  value=""/>
					<#if method == 'add'>
						<input type="hidden" name="method" value="addSave"/>
					</#if>
					<#if method == 'edit'>
						<input type="hidden" name="method" value="editSave"/>
					</#if>
					<input type="hidden" name="oldOrgId" value="${orgBean.orgId!''}"/>
					<input type="hidden" name="status" value="${orgBean.status!''}"/>
					<input type="hidden" id="cityCode" name="cityCode" value="${orgBean.cityCode!''}"/>
					<!-- list页面搜索条件 End -->
					<!-- 添加信息 Form Start -->
					<div class="main-container">
						<div class="layui-form-item">
							<label class="layui-form-label">机构编号</label>
							<div class="layui-input-block">
								<input type="hidden" name="orgId" value="${orgBean.orgId!''}"/>
								<span class="layui-input">${orgBean.orgId!''}</span>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label"><span class="redSpan">*</span>机构名称</label>
							<div class="layui-input-block">
								<input type="text" name="orgName" value="${orgBean.orgName!''}" isValidate="true" notEmpty="true" leng="100" placeholder="请输入机构名称" class="layui-input" lay-verify="title"/>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label"><span class="redSpan">*</span>机构级别</label>
							<div class="layui-input-block">
								<#if method == 'add'>
									<input type="tel" name="level" id="org_level" value="${orgBean.level!''}" isValidate="true" isNum="true" leng="200" placeholder="请输入机构级别(确定后不可修改)" class="layui-input" lay-verify="title"/>
								</#if>
								<#if method == 'edit'>
									<span class="layui-input">${orgBean.level!''}</span>
									<input type="hidden" name="level" value="${orgBean.level!''}"/>
								</#if>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label"><span class="redSpan">*</span>父级机构编号</label>
							<div class="layui-input-inline" >
								<#if method == 'add'>
									<select name="porgId">
										<option>请选择</option>
										<#if porgList??>
											<#list porgList as item>
												<option value="${item.orgId!}" <#if item.orgId == orgBean.porgId>selected</#if>>${item.orgName!}</option>
											</#list>
										</#if>
									</select>
								</#if>
								<#if method == 'edit'>
									<span class="layui-input">${porgBean.orgName!''}</span>
									<input type="hidden" name="porgId" value="${porgBean.orgId!''}"/>
								</#if>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label"><span class="redSpan">*</span>机构类型</label>
							<div class="layui-input-inline" >
								<select name="orgType">
									<option>请选择</option>
									<#if typeList??>
										<#list typeList as item>
											<option value="${item.code!}" <#if item.code == orgBean.orgType!''>selected</#if>>${item.name!}</option> 
										</#list>
									</#if>
								</select>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">联系电话</label>
							<div class="layui-input-block">
								<input type="text" name="phone" value="${orgBean.phone!''}" isValidate="true" isPhone="true" leng="20" placeholder="请输入联系电话" class="layui-input" lay-verify="title"/>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">省市</label>
							<div class="layui-input-inline">
								<select name="province" id="province" lay-filter="changeRegions">
									<option value="">-请选择省份-</option>
									<@city pcityCode="0">
										<#list cityList as item>
											<option value="${item.code}">${item.name}</option>
										</#list>
									</@city>
								</select>
							</div>
							<div class="layui-input-inline">
								<select name="city" id="city">
									<option value="">-请选择-</option>
								</select>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">单位地址</label>
							<div class="layui-input-block">
								<input type="text" value="${orgBean.orgAddr!''}" isValidate="true" leng="100" placeholder="请输入单位地址" class="layui-input" lay-verify="title"/>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">邮编</label>
							<div class="layui-input-block">
								<input type="text" name="zipCode" value="${orgBean.zipCode!''}" isValidate="true" leng="200" placeholder="请输入邮编" class="layui-input" lay-verify="title"/>
							</div>
						</div>
					</div>
					<!-- 添加信息 Form Start -->
					<!-- 按钮区域 Start -->
					<div class="main-button">
						<div class="layui-input-block">
							<#if (Session.CUR_MENU.addRole==1 && method=='add') || (Session.CUR_MENU.updateRole==1 && method=='edit') >
								<input type="button" value="保存" class="layui-btn" onclick="save();"/>
							</#if>
							<input type="button" class="layui-btn layui-btn-primary" value="返回" onClick="goBack();"/>
						</div>
					</div>
					<!-- 按钮区域 End -->
				</form>
			</div>
		</div>
	</body>
</html>